package com.gitee.wsl.compose.ui.datatable.paging

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material3.Divider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextOverflow
//import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.gitee.wsl.compose.icon.filled.Forward
import com.gitee.wsl.compose.icon.filled.ForwardEnd
import com.gitee.wsl.compose.icon.filled.RotateLeft
import com.gitee.wsl.compose.icon.filled.RotateRight
import com.gitee.wsl.compose.ui.datatable.DataColumn
import com.gitee.wsl.compose.ui.datatable.DataTableScope
import com.gitee.wsl.compose.ui.datatable.Material3CellContentProvider
import kotlin.math.min

@Composable
fun PaginatedDataTable(
    columns: List<DataColumn>,
    modifier: Modifier = Modifier,
    separator: @Composable (rowIndex: Int) -> Unit = { Divider() },
    headerHeight: Dp = 56.dp,
    rowHeight: Dp = 52.dp,
    horizontalPadding: Dp = 16.dp,
    state: PaginatedDataTableState = rememberPaginatedDataTableState(10),
    sortColumnIndex: Int? = null,
    sortAscending: Boolean = true,
    content: DataTableScope.() -> Unit,
) {
    BasicPaginatedDataTable(
        columns = columns,
        modifier = modifier,
        separator = separator,
        headerHeight = headerHeight,
        horizontalPadding = horizontalPadding,
        state = state,
        footer = {
            Row(
                modifier = Modifier.height(rowHeight).padding(horizontal = 16.dp).fillMaxWidth(),
                horizontalArrangement = Arrangement.spacedBy(16.dp, alignment = Alignment.End),
                verticalAlignment = Alignment.CenterVertically,
            ) {
                val start = min(state.pageIndex * state.pageSize + 1, state.count)
                val end = min(start + state.pageSize - 1, state.count)
                val pageCount = (state.count + state.pageSize - 1) / state.pageSize
                Text("$start-$end of ${state.count}")
                IconButton(
                    onClick = { state.pageIndex = 0 },
                    enabled = state.pageIndex > 0,
                ) {
                    Icon(Icons.Filled.Forward, "First")
                }
                IconButton(
                    onClick = { state.pageIndex-- },
                    enabled = state.pageIndex > 0,
                ) {
                    Icon(Icons.Filled.RotateLeft, "Previous")
                }
                IconButton(
                    onClick = { state.pageIndex++ },
                    enabled = state.pageIndex < pageCount - 1
                ) {
                    Icon(Icons.Filled.RotateRight, "Next")
                }
                IconButton(
                    onClick = { state.pageIndex = pageCount - 1 },
                    enabled = state.pageIndex < pageCount - 1
                ) {
                    Icon(Icons.Filled.ForwardEnd, "Last")
                }
            }
        },
        cellContentProvider = Material3CellContentProvider,
        sortColumnIndex = sortColumnIndex,
        sortAscending = sortAscending,
        content = content
    )
}

/*
@Preview
@Composable
private fun PaginatedDataTableSample(){
    MaterialTheme{
        val rowData = (0 until 100).map { index ->
            DemoData(index + 1f, "Row: $index")
        }
        var sortColumnIndex by remember { mutableStateOf<Int?>(null) }
        var sortAscending by remember { mutableStateOf(true) }

        val sortedData = when (sortColumnIndex) {
            null -> rowData
            0 -> if (sortAscending) rowData.sortedBy { it.text } else rowData.sortedByDescending { it.text }
            1 -> if (sortAscending) rowData.sortedBy { it.value } else rowData.sortedByDescending { it.value }
            else -> throw IllegalStateException("Invalid column index")
        }

        PaginatedDataTable(
            columns = listOf(
                DataColumn(
                    onSort = { index, ascending ->
                        sortColumnIndex = index
                        sortAscending = ascending
                    }
                ) {
                    Text("Column1")
                },
                DataColumn(
                    alignment = Alignment.End,
                    onSort = { index, ascending ->
                        sortColumnIndex = index
                        sortAscending = ascending
                    }
                ) {
                    Text("Column2")
                },
            ),
            state = rememberPaginatedDataTableState(5),
            sortColumnIndex = sortColumnIndex,
            sortAscending = sortAscending,
            modifier = Modifier.verticalScroll(rememberScrollState()).fillMaxWidth(),
        ) {
            sortedData.forEachIndexed { index, data ->
                row(onClick = { println(index) }) {
                    cell {
                        Text(data.text, maxLines = 1, overflow = TextOverflow.Ellipsis)
                    }
                    cell {
                        Text(data.value.toString())
                    }
                }
            }
        }
    }
}

private data class DemoData(val value: Float, val text: String)*/
